package com.ygxsk.carhome.ui.evaluate;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ColorUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.lxj.xpopup.core.BottomPopupView;
import com.lxj.xpopup.util.XPopupUtils;
import com.ygxsk.carhome.R;
import com.ygxsk.carhome.constant.ConstantSign;
import com.ygxsk.carhome.constant.ExtraValue;
import com.ygxsk.carhome.listener.RxBus;
import com.ygxsk.carhome.response.EvaluateProblemInfo;
import com.ygxsk.carhome.rxbus.RxBusCarProblem;
import com.ygxsk.carhome.utils.CommUtils;
import com.ygxsk.carhome.views.MyLinearLayoutManager;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/*
 * @Author:itchenqi175@163.com
 * @Time:2023-07-12  10:41
 * @Description:跟进状态的布局；
 */
public class EvaluateProblemPopu extends BottomPopupView {

    public EvaluateProblemPopu(@NonNull Context context) {
        super(context);
    }

    //因为通用界面，标志从哪个页面过来；
    private int extra_jumpfrom_where;

    private ArrayList<EvaluateProblemInfo> evaluate_problem_list_outSide = new ArrayList<>();

    //对于客户的跟进状态的适配器
    private EvaluateProblemInfoAdapter evaluateProblemInfoAdapter;

    //弹窗的标题
    private String titleName;

    public void setTitleName(String _titleName) {
        titleName = _titleName;
    }


    public EvaluateProblemPopu(@NonNull Context context, int _extra_action, ArrayList<EvaluateProblemInfo> _stationDatas) {
        super(context);

        //不这样等于，直接 赋值 添加；
        //station_list_outSide = _stationDatas;
        extra_jumpfrom_where = _extra_action;

        if (_stationDatas != null && !_stationDatas.isEmpty()) {

            if (evaluate_problem_list_outSide != null && !evaluate_problem_list_outSide.isEmpty()) {
                evaluate_problem_list_outSide.clear();
            }

            if (evaluate_problem_list_outSide != null) {
                evaluate_problem_list_outSide.addAll(_stationDatas);
            }

        }

    }


    @Override
    protected int getImplLayoutId() {
        return R.layout.populeadsfromwhere;
    }

    @Override
    protected void onCreate() {
        super.onCreate();

//        LogUtils.d(ConstantSign.LOGCAT_NOMAL + "onCreate--onCreate---onCreate--onCreate--onCreate");

        //取消按钮
        TextView btn_dialog_search_cancel = (TextView) this.findViewById(R.id.btn_dialog_search_cancel);
        //确定按钮
        TextView btn_dialog_search_decide = (TextView) this.findViewById(R.id.btn_dialog_search_decide);

        //中间的标题内容
        TextView titleNameWhereMiddle = (TextView) this.findViewById(R.id.titlenamewheremiddle);
        if (titleNameWhereMiddle != null) {
            titleNameWhereMiddle.setText(titleName);
        }

        //初始化适配器列表
        RecyclerView recyclerViewInStation = (RecyclerView) this.findViewById(R.id.recyclerviewinstationasset);
        MyLinearLayoutManager myLinearLayoutManager = new MyLinearLayoutManager(this.getContext(), LinearLayoutManager.VERTICAL, false);
        recyclerViewInStation.setLayoutManager(myLinearLayoutManager);

        //适配器的创建；页面适配器使用的是一个条目项布局
        evaluateProblemInfoAdapter = new EvaluateProblemInfoAdapter(R.layout.popustationinfoadapter, evaluate_problem_list_outSide);
        recyclerViewInStation.setAdapter(evaluateProblemInfoAdapter);

        if (evaluateProblemInfoAdapter != null) {
            evaluateProblemInfoAdapter.notifyDataSetChanged();
        }

        //单独的点击条目
        evaluateProblemInfoAdapter.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {

                //直接设置勾选；
                EvaluateProblemInfo evaluateProblemInfo = (EvaluateProblemInfo) adapter.getItem(position);

                if (evaluateProblemInfo != null) {

                    //发送消息，给业务台账，填充选中的业务类型；
                    for (EvaluateProblemInfo itemBottom : evaluateProblemInfoAdapter.getData()) {

                        //是唯一的；
                        if (TextUtils.equals(evaluateProblemInfo.getValue(), itemBottom.getValue())) {

                            if (itemBottom.isChecked()) {
                                itemBottom.setChecked(false);
                            } else {
                                itemBottom.setChecked(true);
                            }

                            LogUtils.json("itchen--选中的", itemBottom);

                        } else {
                            itemBottom.setChecked(false);
                        }

                    }

                    //是单选，直接刷新；
                    if (evaluateProblemInfoAdapter != null) {
                        evaluateProblemInfoAdapter.notifyDataSetChanged();
                    }

                }
            }
        });

        //【取消】按钮
        if (btn_dialog_search_cancel != null) {
            btn_dialog_search_cancel.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
        }

        //【确定】按钮
        if (btn_dialog_search_decide != null) {
            btn_dialog_search_decide.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {

                    //method_test();

                    //即将被传递的单个item;
                    EvaluateProblemInfo evaluateProblemInfo_upLoad = null;

                    if (evaluate_problem_list_outSide != null && !evaluate_problem_list_outSide.isEmpty()) {

                        //----------------------------------------------------------------------------------------
                        Iterator<EvaluateProblemInfo> iterator = evaluate_problem_list_outSide.iterator();

                        if (iterator != null) {

                            while (iterator.hasNext()) {

                                EvaluateProblemInfo item = iterator.next();

                                if (item != null) {

                                    if (item.isChecked()) {
                                        evaluateProblemInfo_upLoad = item;
                                        LogUtils.json(ConstantSign.LOGCAT_NOMAL + "选中条目项是->", item);
                                        break;
                                    }

                                }
                            }
                        }
                        //----------------------------------------------------------------------------------------
                        if (evaluateProblemInfo_upLoad != null) {

                            //标识是从哪个页面过来的；
                            RxBusCarProblem rxBusCarProblem = new RxBusCarProblem();
                            rxBusCarProblem.setActionFlag(ExtraValue.MODIFY_EVALUATE_CAR_PROBLEM_SELECT);
                            rxBusCarProblem.setEvaluateProblemInfo(evaluateProblemInfo_upLoad);
                            RxBus.getInstance().post(rxBusCarProblem);

                            //关闭该弹窗内容
                            dismiss();

                        } else {
                            //请选择 客户来源；
                            ToastUtils.showShort(R.string.select_evaluate_problem);
                        }

                    } else {
                        //执行异常？
                        ToastUtils.showShort(StringUtils.getString(R.string.action_abnormal));
                    }

                }
            });
        }

    }

    @Override
    protected void onShow() {
        super.onShow();

//        LogUtils.d(ConstantSign.LOGCAT_NOMAL + "界面弹窗 弹出");

    }


    @Override
    public void onDestroy() {
        //        LogUtils.d(ConstantSign.LOGCAT_NOMAL + "弹窗--弹窗--弹窗onDestroy--onDestroy---onDestroy--onDestroy--onDestroy");
        if (evaluate_problem_list_outSide != null && !evaluate_problem_list_outSide.isEmpty()) {

            for (EvaluateProblemInfo evaluateProblemInfo : evaluate_problem_list_outSide) {
                evaluateProblemInfo.setChecked(false);
            }

            if (evaluateProblemInfoAdapter != null) {
                evaluateProblemInfoAdapter.notifyDataSetChanged();
            }

        }
        super.onDestroy();

    }

    @Override
    protected boolean onBackPressed() {

//        LogUtils.d(ConstantSign.LOGCAT_NOMAL + "拦截 弹出弹窗的  返回事件");

//        CommUtils.checkDialog(alertViewDialogSure);

//        if (mDatasResources!=null&&!mDatasResources.isEmpty()){
//        }

        return super.onBackPressed();
    }

    @Override
    protected int getMaxHeight() {
//        return (int) (XPopupUtils.getScreenHeight(getContext()) * .6f);
        return (int) (XPopupUtils.getScreenHeight(getContext()) * .4f);
    }

    /*
     * @Author:itchenqi175@163.com
     * @Time:2023-07-12  15:22
     * @Description:选择列表布局的适配器
     */
    //适配器内容：
    private class EvaluateProblemInfoAdapter extends BaseQuickAdapter<EvaluateProblemInfo, BaseViewHolder> {

        public EvaluateProblemInfoAdapter(int layoutResId, @Nullable List<EvaluateProblemInfo> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(@NonNull BaseViewHolder baseViewHolder, EvaluateProblemInfo evaluateProblemInfo) {

            if (evaluateProblemInfo != null) {

                //是否被勾中
                boolean isChecked = evaluateProblemInfo.isChecked();
                //填充名称：
                String typeName = evaluateProblemInfo.getName();

                //填充名称
                TextView textViewStationNameType = (TextView) baseViewHolder.getView(R.id.textviewstationinfoname);
                ImageView checkBoxInStationType = (ImageView) baseViewHolder.getView(R.id.checkboxinstationname);

                //填充名称
                CommUtils.setText(textViewStationNameType, typeName);

                if (isChecked) {//表示选中
                    //字体设置为选中
//                    textViewStationNameType.setTextColor(ColorUtils.getColor(R.color.orangeygxsk));
                    textViewStationNameType.setTextColor(ColorUtils.getColor(R.color.redcomm));
                    checkBoxInStationType.setVisibility(View.VISIBLE);

//                    checkBoxInStation.post(new Runnable() {
//                        @Override
//                        public void run() {
//                            checkBoxInStation.setVisibility(View.VISIBLE);
//                        }
//                    });

                } else {

                    textViewStationNameType.setTextColor(ColorUtils.getColor(R.color.dark));
                    checkBoxInStationType.setVisibility(INVISIBLE);

//                    checkBoxInStation.post(new Runnable() {
//                        @Override
//                        public void run() {
//                            checkBoxInStation.setVisibility(GONE);
//                        }
//                    });

                }

            }

        }
    }

}
